package com.xijia.wuyouwallet.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.StringUtils;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by Administrator on 2017/12/1 0001.
 */
public class BankCvnActivity extends BaseActivity implements View.OnClickListener{

    private ImageView bankCvn_back;
    private EditText bankCvn_cvv,bankCvn_indate;
    private TextView bankCvn_config;

    private String card_id;
    private String card_merch_mobile;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_cvn);
        initView();
    }

    private void initView(){
        bankCvn_back = (ImageView) findViewById(R.id.bankCvn_back);
        bankCvn_back.setOnClickListener(this);
        bankCvn_cvv = (EditText) findViewById(R.id.bankCvn_cvv);
        bankCvn_indate = (EditText) findViewById(R.id.bankCvn_indate);
        bankCvn_config = (TextView) findViewById(R.id.bankCvn_config);
        bankCvn_config.setOnClickListener(this);

        card_id = getIntent().getStringExtra("card_id");
        card_merch_mobile = getIntent().getStringExtra("card_merch_mobile");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bankCvn_back:
                finish();
                break;
            case R.id.bankCvn_config:
                requestData();
                break;
        }
    }

    private void requestData(){
        final String card_cvn2 = bankCvn_cvv.getText().toString().trim();
        if (card_cvn2.isEmpty()){
            showToast("请输入cvn2");
            return;
        }
        if (card_cvn2.length()!=3){
            showToast("cv2错误");
            return;
        }
        String card_validity_date = bankCvn_indate.getText().toString();
        if (StringUtils.isEmpty(card_validity_date)){
            showToast("请输入有效期");
            return;
        }
        if (card_validity_date.length()!=4){
            showToast("有效期错误");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("card_cvn2",card_cvn2);
        map.put("card_validity_date",card_validity_date);
        map.put("card_id",card_id);
        XUtil.Post(UrlConfig.FILL_CVN_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("cvn2 result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    if (card_merch_mobile.equals("")){
                        requestMerchant();
                    }else {
                        BankCvnActivity.this.setResult(Constants.RESULT_DATA);
                        BankCvnActivity.this.finish();
                    }
                }else {
                    showToast(jsonObject.getString("msg"));
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                showToast("服务器出错啦");
                MyLogger.wLog().e("cvn2 onError="+ex.toString());
            }
        });
    }


    private void requestMerchant(){
        Map<String,Object> map = new HashMap<>();
        map.put("user", MyApp.userData.getData().getUser_id());
        map.put("card_id",card_id);
        map.put("token",MyApp.userData.getData().getToken());
        XUtil.Post(UrlConfig.CREATE_MERCHANT_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("createMerchant ="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    BankCvnActivity.this.setResult(Constants.RESULT_DATA);
                    BankCvnActivity.this.finish();
                }else {
                    showToast(jsonObject.getString("msg"));
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                showToast("服务器出错啦");
                MyLogger.wLog().e("createMerchant onError="+ex.toString());
            }
        });
    }


}
